<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

require_once('includes/classes/Upload.php');
require_once('includes/functions/configuration_functions.php');

class EditProductXsellComponent
{
	function EditProductXsellComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'UpdateProduct') 
			return $this->edit($params, $action);
		else
			return $this->query($params);
	}



	function query(&$params) 
	{
		$handle = get_class($this);
		$categoryId = (int)$params['categoryId'];
		$productId = (int)$params['productId'];
		$languageId = (int)$params['languageId'];

		if ($productId > 0)
		{
			$subParams['handle'] = $handle;
			$subParams['languageId'] = $languageId;
			$subParams['productId'] = $productId;
			$subParams['max_rows'] = 1;
			$subParams['return_details'] = true;
			$subParams['include_all_status'] = true;
			$subParams['return_download_if_no_filename'] = true;

			require_once('includes/components/ProductsSearchSubComponent.php');
			$subComponent = new ProductsSearchSubComponent();
			$value = $subComponent->query($subParams);
		}
		else 
		{
			$value{$handle . 'MetaData'}{'image_width_max'}= (int)SMALL_IMAGE_WIDTH;
			$value{$handle . 'MetaData'}{'image_height_max'}= (int)SMALL_IMAGE_HEIGHT;

			$value{$handle}{0}{'categories_id'}= $categoryId;
			$value{$handle}{0}{'status'}= false;
			$value{$handle}{0}{'qty_blocks'}= 1;
		}

		$value{$handle . 'MetaData'}{'number_of_price_breaks_max'}= (int)PRICE_BREAK_NOF_LEVELS;
		$value{$handle . 'MetaData'}{'download_enabled'}= returnBoolean(DOWNLOAD_ENABLED);
		$value{$handle . 'MetaData'}{'download_maxdays'}= (int)DOWNLOAD_MAX_DAYS;
		$value{$handle . 'MetaData'}{'download_maxcount'}= (int)DOWNLOAD_MAX_COUNT;

		$value{$handle}{0}{'error_code'}= $this->errorCode;
		$value{$handle}{0}{'message'}= $this->message;

		product_list(&$value, $handle, $languageId);

		$sqlQuery = 'select px.xsell_id ' .
		'from  ' . TABLE_PRODUCTS_XSELL . ' px ' .
		'where px.products_id='. $productId;

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i=0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle . 'MetaData'}{'out_xsell'}{$i}{'id'}=(int)$row['xsell_id'];
			$i++;
		}

		$sqlQuery = 'select px.products_id ' .
		'from  ' . TABLE_PRODUCTS_XSELL . ' px ' .
		'where px.xsell_id='. $productId;

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i=0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle . 'MetaData'}{'in_xsell'}{$i}{'id'}=(int)$row['products_id'];
			$i++;
		}
				
		return $value;
	}

	function edit(&$params, $action) 
	{
		$categoryId = (int)$params['categoryId'];
		$productId = (int)$params['productId'];
		$languageId = (int)$params['languageId'];
		
		$out_xsell_ids = $params['out_xsell_ids'];
		$in_xsell_ids = $params['in_xsell_ids'];

		//Products XSell
		$out_xsells = explode(',', $out_xsell_ids);
		$in_xsells = explode(',', $in_xsell_ids);

		if ($productId > 0)
		{
			$sqlQuery = 'select px.xsell_id ' .
			'from  ' . TABLE_PRODUCTS_XSELL . ' px ' .
			'where px.products_id='. $productId . ' ';

			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			
			while($row = mysql_fetch_array($dataReturned))
			{
				$xsell_id =(int)$row['xsell_id'];
				$tmpArray = array();
				reset($out_xsells);
				$match = false;
				while (list(, $value) = each($out_xsells)) 
				{
					$intValue = (int)$value;
					if ($intValue == $xsell_id) {
						$match = true;
					}
					else {
						$tmpArray[] = $value;
					}
				}
				$out_xsells = $tmpArray;
				if (!$match) {
					$sql = 'delete from ' . TABLE_PRODUCTS_XSELL . ' where products_id='. $productId . ' and xsell_id=' . $xsell_id;
					mysql_query($sql) or die(mysql_error());
				}
			}

			$sqlQuery = 'select px.products_id ' .
			'from  ' . TABLE_PRODUCTS_XSELL . ' px ' .
			'where px.xsell_id='. $productId . ' ';

			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			
			while($row = mysql_fetch_array($dataReturned))
			{
				$xsell_id =(int)$row['products_id'];
				$tmpArray = array();
				reset($in_xsells);
				$match = false;
				while (list(, $value) = each($in_xsells)) 
				{
					$intValue = (int)$value;
					if ($intValue == $xsell_id) {
						$match = true;
					}
					else {
						$tmpArray[] = $value;
					}
				}
				$in_xsells = $tmpArray;
				if (!$match) {
					$sql = 'delete from ' . TABLE_PRODUCTS_XSELL . ' where products_id='. $xsell_id . ' and xsell_id=' . $productId;
					mysql_query($sql) or die(mysql_error());
					remove_cache($languageId . '_' . $xsell_id, 'ProductXSellComponent');
				}
			}

			reset($out_xsells);
			while (list(, $value) = each($out_xsells)) 
			{
				$intValue = (int)$value;
				if ($intValue > 0) {
					$sql_data_array = array();
					$sql_data_array['products_id'] = $productId;
					$sql_data_array['xsell_id'] = $intValue;
					$sql_data_array['sort_order'] = 1;
					db_perform(TABLE_PRODUCTS_XSELL, $sql_data_array, 'insert', '');
				}
			}

			reset($in_xsells);
			while (list(, $value) = each($in_xsells)) 
			{
				$intValue = (int)$value;
				if ($intValue > 0) {
					$sql_data_array = array();
					$sql_data_array['products_id'] = $intValue;
					$sql_data_array['xsell_id'] = $productId;
					$sql_data_array['sort_order'] = 1;
					db_perform(TABLE_PRODUCTS_XSELL, $sql_data_array, 'insert', '');
					remove_cache($languageId . '_' . $intValue, 'ProductXSellComponent');
				}
			}

			remove_cache($languageId . '_' . $productId, 'ProductXSellComponent');
		}

		$params['productId'] = $productId;
		return $this->query($params);
	}
}
?>

